package com.dc.project.sales.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.common.annotation.DataScope;
import com.dc.common.utils.PageUtil;
import com.dc.common.vo.ResultVo;
import com.dc.project.sales.dao.SalOrderDetailDao;
import com.dc.project.sales.dto.SalOrderDetailDto;
import com.dc.project.sales.entity.SalOrderDetail;
import com.dc.project.sales.service.SalOrderDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 销售订单明细表(SalOrderDetail)表服务实现类
 *
 * @author zhuangcy
 * @since 2021-06-12 16:37:12
 */
@Service("salOrderDetailService")
public class SalOrderDetailServiceImpl extends ServiceImpl<SalOrderDetailDao, SalOrderDetail> implements SalOrderDetailService {

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return
     */
    @Override
    public ResultVo queryById(Serializable id) {
        return ResultVo.success().data(this.getById(id));
    }

    /**
     * 分页查询销售订单明细表
     *
     * @param dto
     * @return
     */
    @DataScope
    @Override
    public ResultVo queryByPage(SalOrderDetailDto dto) {
        QueryWrapper<SalOrderDetail> queryWrapper = new QueryWrapper<>();
        IPage<SalOrderDetail> page = this.page(PageUtil.convert2PageParam(dto), queryWrapper);
        return ResultVo.success().data(PageUtil.convert2PageResult(page));
    }

    /**
     * 新增销售订单明细表
     *
     * @param salOrderDetail
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo add(SalOrderDetail salOrderDetail) {
        return ResultVo.verify(this.save(salOrderDetail));
    }

    /**
     * 编辑销售订单明细表
     *
     * @param salOrderDetail
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo edit(SalOrderDetail salOrderDetail) {
        return ResultVo.verify(this.updateById(salOrderDetail));
    }

    /**
     * 根据主键删除销售订单明细表
     *
     * @param id 主键
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo delete(Serializable id) {
        return ResultVo.verify(this.removeById(id));
    }

    /**
     * 查询销售订单明细表列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResultVo queryByList(SalOrderDetailDto dto) {
        LambdaQueryWrapper<SalOrderDetail> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SalOrderDetail::getOrderId, dto.getOrderId());
        queryWrapper.orderByAsc(SalOrderDetail::getOrderDetailId);
        List<SalOrderDetail> list = this.list(queryWrapper);
        return ResultVo.success().data(list);
    }
}